package com.lenovo.manufacture;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FinishedVehicle extends AppCompatActivity {
    private ListView listView;
    private TextView tvcapacity;
    private Button btReturn;
    MyAdapter myAdapter;
    private List<ProblemData1.DataBean> data1=new ArrayList<>();
    private List<WarehouseData> data2=new ArrayList<>();
    private Retrofit retrofit=new Retrofit.Builder()
            .baseUrl("http://hh1.me:5001/Interface/")
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_finished_vehicle);
        setVehicle();
        initview();
        btReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private void initview() {
        listView=findViewById(R.id.ll_vehicle);
        tvcapacity=findViewById(R.id.tv_capacity);
        btReturn =findViewById(R.id.bt_return);
    }

    private void setVehicle (){
        Service service=retrofit.create(Service.class);
        Call<ProblemData1> getallcar=service.getAllCar();
        getallcar.enqueue(new Callback<ProblemData1>() {
            @Override
            public void onResponse(Call<ProblemData1> call, Response<ProblemData1> response) {
                for (ProblemData1.DataBean databen:response.body().getData()){
                    data1.add(databen);
                }

                Service service1=retrofit.create(Service.class);
                Call<WarehouseData> iswarehouse =service1.isWareHouse();
                iswarehouse.enqueue(new Callback<WarehouseData>() {
                    @Override
                    public void onResponse(Call<WarehouseData> call, Response<WarehouseData> response) {
                        data2.add(response.body());

                        Service service2=retrofit.create(Service.class);
                        Call<StudentFactoryData> isfactory=service2.isFactory(1);
                        isfactory.enqueue(new Callback<StudentFactoryData>() {
                            @Override
                            public void onResponse(Call<StudentFactoryData> call, Response<StudentFactoryData> response) {
                                int k=0;
                                for (int i=0;i<data1.size();i++){
                                    k+=data1.get(i).getSize();
                                }
                                tvcapacity.setText("成品车辆库存信息：共占地"+k+"，库存容量"+response.body().getData().get(0).getCapacity2());
                            }

                            @Override
                            public void onFailure(Call<StudentFactoryData> call, Throwable t) {
                            }
                        });
                        myAdapter=new MyAdapter();
                        listView.setAdapter(myAdapter);
                    }
                    @Override
                    public void onFailure(Call<WarehouseData> call, Throwable t) {

                    }
                });
            }
            @Override
            public void onFailure(Call<ProblemData1> call, Throwable t) {

            }
        });
    }

    private class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return data1.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView= LayoutInflater.from(FinishedVehicle.this).inflate(R.layout.row_vehicle,parent,false);
            TextView tvid=(TextView) convertView.findViewById(R.id.tv_id);
            TextView tvtype=(TextView) convertView.findViewById(R.id.tv_type);
            TextView tvnumber=(TextView) convertView.findViewById(R.id.tv_number);
            TextView tvarea=(TextView) convertView.findViewById(R.id.tv_area);
            TextView tvline=(TextView) convertView.findViewById(R.id.tv_line);
            TextView tvqualified=(TextView) convertView.findViewById(R.id.tv_qualified);
            tvid.setText(data1.get(position).getId()+"");
            tvtype.setText(data1.get(position).getCarTypeName());
            tvnumber.setText(data2.get(0).getData().get(0).getNum()+"");
            tvline.setText(data2.get(0).getData().get(0).getUserLineId()+"");
            tvarea.setText(data1.get(position).getSize()+"");
            tvqualified.setText("维修");
            return convertView;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}